package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.config.JwtUtils;
import com.ccc.demoboot.pojo.*;
import com.ccc.demoboot.service.CodeService;
import com.ccc.demoboot.service.StorageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author wz
 * @Date 2024/6/25 8:42
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: StorageController
 */
@RequestMapping("/storage")
@RestController
public class StorageController {
    @Autowired
    StorageService storageService;

    @Autowired
    CodeService codeService;

    /**
     * 物资入库申请添加
     *
     * @param storage
     * @return
     */
    @PostMapping("/addsto")
    public R addSto(@RequestBody Storage storage, @RequestHeader("jwttoken") String jwttoken) {
        Integer jwttoken1 = JwtUtils.getJwtToken(jwttoken, "id");
        storage.setUserid(jwttoken1);
        storage.setApplicationtime(new Date());
        storage.setStatus(2);
        boolean save = this.storageService.save(storage);
        return new R().add("save", save);
    }

    /**
     * 查询物资编码申请
     *
     * @param map
     * @param jwttoken
     * @return
     */
    @PostMapping("/selectStorage")
    public R selectAllStorage(@RequestBody Map<String, Object> map, @RequestHeader("jwttoken") String jwttoken) {
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        int status = Integer.valueOf(map.get("status").toString());//获取订单状态
        int page = (int) map.get("page");//获取当前页码
        int pageSize = (int) map.get("pageSize");//获取每页条数
        PageHelper.startPage(page, pageSize);//第一个参数表示第几页,第二个参数表示每页的条数
        String codematerial = (String) map.get("codematerial");
        String materialname = (String) map.get("materialname");
        QueryWrapper<Map> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("materialname", materialname);
        queryWrapper.like("codematerial", codematerial);
        List<Map<String, Object>> storageList = storageService.selectAllStorage(queryWrapper, status,applyuserid);
        PageInfo<Map<String, Object>> list = new PageInfo<>(storageList);
        return new R().add("list", list);
    }

    /**
     * 根据id查询详情
     *
     * @param map
     * @param jwttoken
     * @return
     */
    @PostMapping("/selectOneStorage")
    public R selectOneStorage(@RequestBody Map<String, Object> map, @RequestHeader("jwttoken") String jwttoken) {
        int id = (int) map.get("id");
        Map<String, Object> one = storageService.selectOne(id);
        return new R().add("one", one);
    }

    /**
     * 修改订单状态，0不通过 1通过 3取消
     *
     * @param mapOne
     * @param jwttoken
     * @return
     */
    @PostMapping("/upStorage")
    public R upDer(@RequestBody Map<String, Object> mapOne, @RequestHeader("jwttoken") String jwttoken) {
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        Map<String, Object> map = (Map<String, Object>) mapOne.get("map");
        int status = Integer.valueOf(map.get("status").toString());//获取订单状态
        int id = Integer.valueOf(map.get("id").toString());//获取要修改的订单id
        String codematerial = (String) map.get("codematerial");//获取物资编码
        String materialname = (String) map.get("materialname");//获取物资
        Storage storage = new Storage();
        if (status == 1) {
            int result = codeService.selectOne(codematerial);
            if (result > 0) {
                return new R().setCode(400).setMessage("物资编码已存在，请查证后再试");
            } else {
                int num = codeService.addCode(codematerial, materialname);
                if (num > 0) {
                    storage.setId(id);
                    storage.setThroughtime(new Date());
                    storage.setReviewerid(applyuserid);
                    storage.setStatus(status);
                    storageService.updateById(storage);
                    return new R().setCode(403).setMessage("审核成功");
                }
            }
        } else {
            storage.setId(id);
            storage.setThroughtime(new Date());
            storage.setReviewerid(applyuserid);
            storage.setStatus(status);
            storageService.updateById(storage);
            return new R().setCode(500).setMessage(status == 0 ? "取消成功" : "操作成功");
        }
        //系统出错
        return new R().setCode(500).setMessage("系统出错，请联系管理员");
    }
}
